package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.GradeInformation;
import com.mt.roll.core.dal.service.GradeInformationServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.GradeInformationParam;
import com.mt.roll.wms.params.GradeInformationQueryParam;
import com.mt.roll.wms.service.GradeInformationService;
import com.mt.roll.wms.vo.GradeInformationVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author weihongyang
 * @Date 2021/11/1 11:05 上午
 * @Version 1.0
 */
@Transactional
@Service
public class GradeInformationServiceImpl extends BaseService implements GradeInformationService {

    @Autowired
    private GradeInformationServiceBiz gradeInformationServiceBiz;

    @Override
    public R<IdVo> add(GradeInformationParam gradeInformationParam) {
        QueryWrapper<GradeInformation> wrapper = new QueryWrapper<>();
        wrapper.eq(GradeInformation.EQUIPMENT_ID, gradeInformationParam.getEquipment_id())
                .eq(GradeInformation.GRADE, gradeInformationParam.getGrade());
        Assert.eqZero(gradeInformationServiceBiz.count(wrapper), "设备该等级已经存在");
        GradeInformation gradeInformation = new GradeInformation();
        BeanUtils.copyProperties(gradeInformationParam, gradeInformation);
        setCommonField(gradeInformation);
        gradeInformationServiceBiz.save(gradeInformation);
        return successful(IdVo.builder().id(gradeInformation.getId()).build());
    }

    @Override
    public R<IdVo> update(GradeInformationParam gradeInformationParam) {
        GradeInformation gradeInformation = gradeInformationServiceBiz.getById(gradeInformationParam.getId());
        if (!gradeInformation.getEquipmentId().equals(gradeInformationParam.getEquipment_id()) || !gradeInformation.getGrade().equals(gradeInformationParam.getGrade())) {
            QueryWrapper<GradeInformation> wrapper = new QueryWrapper<>();
            wrapper.eq(GradeInformation.EQUIPMENT_ID, gradeInformationParam.getEquipment_id())
                    .eq(GradeInformation.GRADE, gradeInformationParam.getGrade());
            Assert.eqZero(gradeInformationServiceBiz.count(wrapper), "设备该等级已经存在");
        }
        GradeInformation updateAlarm = new GradeInformation();
        BeanUtils.copyProperties(gradeInformationParam, updateAlarm);
        setUpdateCommonField(updateAlarm);
        gradeInformationServiceBiz.updateById(updateAlarm);
        return successful(IdVo.builder().id(gradeInformation.getId()).build());
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        GradeInformation gradeInformation = gradeInformationServiceBiz.getById(idParam.getId());
        if (gradeInformation == null) {
            return failed("等级信息不存在");
        }
        GradeInformation updateAlarm = new GradeInformation().setId(gradeInformation.getId());
        updateAlarm.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateAlarm);
        gradeInformationServiceBiz.updateById(updateAlarm);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        gradeInformationServiceBiz.removeById(updateAlarm.getId());
        return successful(IdVo.builder().id(gradeInformation.getId()).build());
    }

    @Override
    public R<PageVo<GradeInformationVo>> page(GradeInformationQueryParam gradeInformationQueryParam) {
        QueryWrapper<GradeInformation> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(gradeInformationQueryParam.getEquipmentName()), GradeInformation.EQUIPMENT_NAME, gradeInformationQueryParam.getEquipmentName())
                .orderByDesc(GradeInformation.ID);
        IPage<GradeInformation> page = gradeInformationServiceBiz.page(new Page<>(gradeInformationQueryParam.getCurrent(), gradeInformationQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, GradeInformationVo.class));
    }
}
